package com.soar.recruitment.module.launcher.ui.fragment;

import android.os.Bundle;

import com.soar.recruitment.R;
import com.soar.recruitment.common.dal.event.BaseEvent;
import com.soar.recruitment.common.dal.event.StartBrotherEvent;
import com.soar.recruitment.common.ui.fragment.SOARBaseMainFragment;
import com.soar.recruitment.common.ui.view.tab.BottomBar;
import com.soar.recruitment.common.ui.view.tab.BottomBarTab;
import com.soar.recruitment.common.ui.view.tab.OnLoginListener;
import com.soar.recruitment.module.attention.ui.fragment.AttentionFragment;
import com.soar.recruitment.module.home.ui.fragment.HomeFragment;
import com.soar.recruitment.module.info.ui.fragment.InfoFragment;
import com.soar.recruitment.module.me.ui.fragment.MeFragment;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;


public class MainFragment extends SOARBaseMainFragment {

    public static final int TAB_HOME = 0;
    public static final int TAB_INFO = 1;
    public static final int TAB_ATTENTION = 2;
    public static final int TAB_ME = 3;

    private SupportFragment[] mFragments = new SupportFragment[4];

    @BindView(R.id.bottomBar)
    BottomBar mBottomBar;

    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_main;
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            mFragments[TAB_HOME] = HomeFragment.newInstance();
            mFragments[TAB_INFO] = InfoFragment.newInstance();
            mFragments[TAB_ATTENTION] = AttentionFragment.newInstance();
            mFragments[TAB_ME] = MeFragment.newInstance();

            loadMultipleRootFragment(R.id.fl_tab_container, TAB_HOME,
                    mFragments[TAB_HOME],
                    mFragments[TAB_INFO],
                    mFragments[TAB_ATTENTION],
                    mFragments[TAB_ME]);
        } else {
            mFragments[TAB_HOME] = findChildFragment(HomeFragment.class);
            mFragments[TAB_INFO] = findChildFragment(InfoFragment.class);
            mFragments[TAB_ATTENTION] = findChildFragment(AttentionFragment.class);
            mFragments[TAB_ME] = findChildFragment(MeFragment.class);
        }

        BottomBarTab homeTab = new BottomBarTab(_mActivity, R.drawable.tab_home_selector, getString(R.string.tab_home));
        BottomBarTab infoTab = new BottomBarTab(_mActivity, R.drawable.tab_info_selector, getString(R.string.tab_info));
        BottomBarTab attentionTab = new BottomBarTab(_mActivity, R.drawable.tab_attention_selector, getString(R.string.tab_attention));
        BottomBarTab meTab = new BottomBarTab(_mActivity, R.drawable.tab_me_selector, getString(R.string.tab_me));

        mBottomBar
                .addItem(homeTab, null)
                .addItem(infoTab, null)
                .addItem(attentionTab, new OnLoginListener() {
                    @Override
                    public boolean loginStatus() {
                        /*if(!LoginManager.isLogin()) {
                            LoginManager.jumpToLogin(mActivity);
                            return false;
                        }*/
                        return true;
                    }
                })
                .addItem(meTab, null);

        homeTab.setSelected(true);
        infoTab.setSelected(false);
        attentionTab.setSelected(false);
        meTab.setSelected(false);

        mBottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);

                BottomBarTab tab = mBottomBar.getItem(TAB_HOME);
                tab.setUnreadCount(0);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    /**
     * start other BrotherFragment
     */
    @Subscribe
    public void startBrother(BaseEvent event) {
        if(event instanceof StartBrotherEvent) {
            StartBrotherEvent startBrotherEvent = (StartBrotherEvent)event;
            if(startBrotherEvent.homeOpen) {
                start(startBrotherEvent.targetFragment);
            }
        }
    }

    @Override
    public void initData() {

    }

}
